﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

/// <summary>
/// 配置文件读取帮助类
/// </summary>
public class ConfigHelper
{
	/// <summary>
	/// 在配置文件中获得字符串
	/// </summary>
	/// <param name="key">键</param>
	/// <param name="defaultValue">默认值</param>
	/// <returns></returns>
	public static string GetString(string key, string defaultValue)
	{
		string value = defaultValue;
		if (ConfigurationManager.AppSettings[key] != null)
		{
			value = ConfigurationManager.AppSettings[key];
		}
		return value;
	}

	/// <summary>
	/// 在配置文件中获得布尔值
	/// </summary>
	/// <param name="key">键</param>
	/// <param name="defaultValue">默认值</param>
	/// <returns></returns>
	public static bool GetBoolean(string key, bool defaultValue)
	{
		bool value = defaultValue;
		if (ConfigurationManager.AppSettings[key] != null)
		{
			switch (ConfigurationManager.AppSettings[key].ToLower())
			{
				case "1":
				case "y":
				case "true":
					value = true;
					break;
				default:
					value = false;
					break;
			}
		}
		return value;
	}

	/// <summary>
	/// 在配置文件中获得整型值
	/// </summary>
	/// <param name="key">键</param>
	/// <param name="defaultValue">默认值</param>
	/// <returns></returns>
	public static int GetInteger(string key, int defaultValue)
	{
		int value = defaultValue;
		if (ConfigurationManager.AppSettings[key] != null)
		{
			int.TryParse(ConfigurationManager.AppSettings[key], out value);
		}
		return value;
	}

	/// <summary>
	/// 在配置文件中获得数字值
	/// </summary>
	/// <param name="key">键</param>
	/// <param name="defaultValue">默认值</param>
	/// <returns></returns>
	public static decimal GetDecimal(string key, decimal defaultValue)
	{
		decimal value = defaultValue;
		if (ConfigurationManager.AppSettings[key] != null)
		{
			Decimal.TryParse(ConfigurationManager.AppSettings[key], out value);
		}
		return value;
	}

	/// <summary>
	/// 在配置文件中获得双精度值
	/// </summary>
	/// <param name="key">键</param>
	/// <param name="defaultValue">默认值</param>
	/// <returns></returns>
	public static double GetDouble(string key, double defaultValue)
	{
		double value = defaultValue;
		if (ConfigurationManager.AppSettings[key] != null)
		{
			Double.TryParse(ConfigurationManager.AppSettings[key], out value);
		}
		return value;
	}

	/// <summary>
	/// 在配置文件中获得Long值
	/// </summary>
	/// <param name="key">键</param>
	/// <param name="defaultValue">默认值</param>
	/// <returns></returns>
	public static long GetLong(string key, long defaultValue)
	{
		long value = defaultValue;
		if (ConfigurationManager.AppSettings[key] != null)
		{
			long.TryParse(ConfigurationManager.AppSettings[key], out value);
		}
		return value;
	}

	/// <summary>
	/// 在配置文件中获得日期时间值
	/// </summary>
	/// <param name="key">键</param>
	/// <param name="defaultValue">默认值</param>
	/// <returns></returns>
	public static DateTime GetDateTime(string key, DateTime defaultValue)
	{
		DateTime value = defaultValue;
		if (ConfigurationManager.AppSettings[key] != null)
		{
			DateTime.TryParse(ConfigurationManager.AppSettings[key], out value);
		}
		return value;
	}
}